<?php

namespace app\admin\model;

use think\model\Relation;

class Department extends Relation {

        protected $link = [
            'member' => [
                'mapping_type' => BELONGS_TO,
                'class_name' => 'Member',
                'foreign_key' => 'leader_id',
                'mapping_fields' => 'username,realname as username1',
                'as_fields' => 'username,username1',
                'parent_key' => 'id'
            ]
        ];

        protected function _after_select(&$resultSet, $options = array()) {
                parent::_after_select($resultSet, $options);
                foreach ($resultSet as $key => $value) {
                if(!empty($value['username'])){
                $value['username'] = $value['username'];
                }else{
                $value['username'] = '';
                }
                        if (empty($value['username1'])) {
                                $resultSet[$key]['username'] = $value['username'];
                        } else {
                                $resultSet[$key]['username'] = $value['username1'];
                        }
                }
        }

}
